﻿--宠物目标
local ToPetFrame = CreateFrame("Button", "ToPetFrame", PetFrame, "SecureUnitButtonTemplate");
ToPetFrame:SetFrameLevel(8);
ToPetFrame:SetWidth(96);
ToPetFrame:SetHeight(48);
ToPetFrame:ClearAllPoints();
ToPetFrame:SetPoint("LEFT", PetFrame, "RIGHT", 0, -10);
ToPetFrame:SetAlpha(0);

ToPetFrame.Portrait = ToPetFrame:CreateTexture("ToPetPortrait", "BORDER");
ToPetFrame.Portrait:SetWidth(27);
ToPetFrame.Portrait:SetHeight(27);
ToPetFrame.Portrait:ClearAllPoints();
ToPetFrame.Portrait:SetPoint("TOPLEFT", ToPetFrame, "TOPLEFT", 6, -5);

ToPetFrame.Texture = ToPetFrame:CreateTexture("ToPetTexture", "ARTWORK");
ToPetFrame.Texture:SetTexture("Interface\\TargetingFrame\\UI-PartyFrame");
ToPetFrame.Texture:SetWidth(96);
ToPetFrame.Texture:SetHeight(48);
ToPetFrame.Texture:ClearAllPoints();
ToPetFrame.Texture:SetPoint("TOPLEFT", ToPetFrame, "TOPLEFT", 0, -2);

ToPetFrame.Name = ToPetFrame:CreateFontString("ToPetName", "ARTWORK", "GameFontNormalSmall");
ToPetFrame.Name:ClearAllPoints();
ToPetFrame.Name:SetPoint("BOTTOMLEFT", ToPetFrame, "BOTTOMLEFT", 33, 39);

ToPetFrame.HealthBar = CreateFrame("StatusBar", "ToPetHealthBar", ToPetFrame);
ToPetFrame.HealthBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToPetFrame.HealthBar:SetFrameLevel(2);
ToPetFrame.HealthBar:SetMinMaxValues(0, 100);
ToPetFrame.HealthBar:SetValue(0);
ToPetFrame.HealthBar:SetWidth(53);
ToPetFrame.HealthBar:SetHeight(6);
ToPetFrame.HealthBar:ClearAllPoints();
ToPetFrame.HealthBar:SetPoint("TOPLEFT", ToPetFrame, "TOPLEFT", 35, -9);
ToPetFrame.HealthBar:SetStatusBarColor(0, 1, 0);

ToPetFrame.HPPct = ToPetFrame:CreateFontString("ToPetHPPct", "ARTWORK", "TextStatusBarText");
ToPetFrame.HPPct:SetFont(GameFontNormal:GetFont(), 10, "OUTLINE");
ToPetFrame.HPPct:SetTextColor(1, 0.75, 0);
ToPetFrame.HPPct:SetJustifyH("LEFT");
ToPetFrame.HPPct:ClearAllPoints();
ToPetFrame.HPPct:SetPoint("LEFT", ToPetFrame.HealthBar, "RIGHT", 2, -4);

ToPetFrame.PowerBar = CreateFrame("StatusBar", "ToPetPowerBar", ToPetFrame);
ToPetFrame.PowerBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar");
ToPetFrame.PowerBar:SetFrameLevel(2);
ToPetFrame.PowerBar:SetMinMaxValues(0, 100);
ToPetFrame.PowerBar:SetValue(0);
ToPetFrame.PowerBar:SetWidth(53);
ToPetFrame.PowerBar:SetHeight(6);
ToPetFrame.PowerBar:ClearAllPoints();
ToPetFrame.PowerBar:SetPoint("TOPLEFT", ToPetFrame, "TOPLEFT", 35, -16);
ToPetFrame.PowerBar:SetStatusBarColor(0, 0, 1);

--允许点击选取目标
ToPetFrame.DropDown = CreateFrame("Button", "ToPetDropDown", ToPetFrame, "UIDropDownMenuTemplate");
ToPetFrame.DropDown:SetWidth(10);
ToPetFrame.DropDown:SetHeight(10);
ToPetFrame.DropDown:ClearAllPoints();
ToPetFrame.DropDown:SetPoint("TOP", ToPetFrame, "TOP", -20, -10);
ToPetFrame.DropDown:Hide();

local showmenu = (function()
	ToggleDropDownMenu(1, nil, ToPetFrame.DropDown, "ToPetFrame", 40, 0);
end)
SecureUnitButton_OnLoad(ToPetFrame, "pettarget", showmenu);

ToPetFrame:RegisterForClicks("AnyUp");
ToPetFrame:RegisterForDrag("LeftButton");

local pettarget = CreateFrame("Frame");
local ToPetNameColor = PowerBarColor["MANA"];
local ToPetPctText = "";
function UnitFramesPlus_PetTarget()
	if UnitFramesPlusDB["player"]["pettarget"] == 1 then
		pettarget:SetScript("OnUpdate", function(self, elapsed)
			self.timer = (self.timer or 0) + elapsed;
			if self.timer >= 0.1 then
				if UnitExists("pettarget") then
					ToPetFrame:SetAlpha(1);

					ToPetFrame.Name:SetText(UnitName("pettarget"));

					ToPetNameColor = PowerBarColor[UnitPowerType("pettarget")] or PowerBarColor["MANA"];
					ToPetFrame.PowerBar:SetStatusBarColor(ToPetNameColor.r, ToPetNameColor.g, ToPetNameColor.b);

					SetPortraitTexture(ToPetFrame.Portrait, "pettarget");

					if UnitHealthMax("pettarget") > 0 then
						ToPetFrame.HealthBar:SetValue(UnitHealth("pettarget") / UnitHealthMax("pettarget") * 100);
						ToPetPctText = "";
						if UnitFramesPlusDB["player"]["pthppct"] == 1 then
							ToPetPctText = math.floor(UnitHealth("pettarget") / UnitHealthMax("pettarget") * 100).."%";
						end
						ToPetFrame.HPPct:SetText(ToPetPctText);
					else
						ToPetFrame.HealthBar:SetValue(0);
						ToPetFrame.HPPct:SetText("");
					end

					if UnitPowerMax("pettarget") > 0 then
						ToPetFrame.PowerBar:SetValue(UnitPower("pettarget") / UnitPowerMax("pettarget") * 100);
					else
						ToPetFrame.PowerBar:SetValue(0);
					end
				else
					ToPetFrame:SetAlpha(0);
					ToPetFrame.HealthBar:SetValue(0);
					ToPetFrame.PowerBar:SetValue(0);
					ToPetFrame.HPPct:SetText("");
				end
				self.timer = 0;
			end
		end);
	else
		ToPetFrame:SetAlpha(0);
		ToPetFrame.HealthBar:SetValue(0);
		ToPetFrame.PowerBar:SetValue(0);
		ToPetFrame.HPPct:SetText("");
		pettarget:SetScript("OnUpdate", nil);
	end
end

--宠物目标缩放
function UnitFramesPlus_PetTargetScale(oldscale, newscale)
	local oldscale = oldscale or UnitFramesPlusDB["player"]["ptscale"];
	local newscale = newscale or UnitFramesPlusDB["player"]["ptscale"];
	if UnitFramesPlusDB["player"]["pettarget"] == 1 and (not InCombatLockdown()) then
		local point, relativeTo, relativePoint, offsetX, offsetY = ToPetFrame:GetPoint();
		ToPetFrame:SetScale(newscale);
		ToPetFrame.HealthBar:SetScale(newscale);
		ToPetFrame.PowerBar:SetScale(newscale);
		ToPetFrame:ClearAllPoints();
		ToPetFrame:SetPoint(point, relativeTo, relativePoint, offsetX*oldscale/newscale, offsetY*oldscale/newscale);
	end
end

--模块初始化
function UnitFramesPlus_PetTargetInit()
	UnitFramesPlus_PetTarget();
	UnitFramesPlus_PetTargetScale();
end
